const express = require('express');
const mysql = require('mysql');
const sign = require('../../libs/common-util');
const config = require('../../config');

// const db = mysql.createPool({
//     host : 'localhost',
//     user : 'root',
//     password : 'leeyao22',
//     database : 'learner'
// });

const db = mysql.createPool({
    host : config.databaseHost,//,'localhost',
    user : config.databaseUser,//'root',
    password : config.databasePwd,//'Deng%)#@real5032',
    database : config.database,//'learner'
});

module.exports = function() {
    var bannersRouter = express.Router();

    bannersRouter.get('/', (req, res)=> {
        switch(req.query.act) {
            case 'update':
            db.query(`SELECT * FROM banner_table WHERE ID='${req.query.id}'`, (err, updates)=>{
                if (err) {
                    console.log(err);
                    res.status(500).send('database error.').end();
                }else {
                    // res.render('admin/banners.ejs', );
                    if (updates.length == 0) {
                        res.status(404).send('no banner ID').end();
                    }else {
                        db.query(`SELECT * FROM banner_table`, (err, data)=>{
                            if (data) {
                                console.log(data);
                                res.render('admin/banners.ejs', {details : data, updateDetail : updates[0]});
                            }else {
                                console.error(err);
                                res.render('admin/banners.ejs');
                            }
                        });
                    }
                    
                }
            });
            break;
            case 'del':
                db.query(`DELETE FROM banner_table WHERE ID='${req.query.id}'`, (err, data)=>{
                    if (err) {
                        console.log(err);
                        res.status(500).send('database error.').end();
                    }else {
                        res.redirect('/admin/banners');
                    }
                });
            break;
            default:
                db.query(`SELECT * FROM banner_table`, (err, data)=>{
                    if (data) {
                        console.log(data);
                        res.render('admin/banners.ejs', {details : data});
                    }else {
                        console.error(err);
                        res.render('admin/banners.ejs');
                    }
                });
            break;
        }
    });

    bannersRouter.post('/', (req, res)=> {
        console.log(req.body);
        let title = req.body.title;
        let desc = req.body.desc;
        let href = req.body.href;
        if (!title || !desc || !href) {
            res.status(400).send('信息不能为空').end();
        } else {
            if (req.body.update_id) { //修改
                db.query(`UPDATE banner_table SET title='${title}',\
                description='${desc}',\
                href='${href}' WHERE ID='${req.body.update_id}'`, (err, updateDetail)=>{
                    if (err) {
                        console.error(err);
                        res.status(500).send('数据库错误').end();
                    }else {
                        console.log(updateDetail);
                        res.redirect('/admin/banners');
                    }
                });
            }else { //添加
                db.query(`INSERT INTO banner_table (title, description, href) value('${title}', '${desc}', '${href}')`, (err, data)=> {
                    if (err) {
                        console.error(err);
                        res.status(500).send('数据库错误').end();
                    }else {
                        console.log(data);
                        res.redirect('/admin/banners');
                    }
                });
            }
        }
    });

    return bannersRouter;
}